package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysUserRoleDO;
import com.andy.blog.dal.mapper.SysUserRoleMapper;
import com.andy.blog.dal.service.SysUserRoleDalService;
import com.andy.blog.dal.vo.SysUserRoleVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created by andy on 2023/8/12 21:09
 */
@Service
public class SysUserRoleDalServiceImpl implements SysUserRoleDalService {

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    public void deleteUserRoleByUserId(Long userId) {
        if (Objects.isNull(userId)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "用户Id不能为空");
        }

        try {
            sysUserRoleMapper.deleteUserRoleByUserId(userId);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102, e);
        }
    }

    @Override
    public void batchUserRole(List<SysUserRoleDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "入参不能为空");
        }

        try {
            sysUserRoleMapper.batchUserRole(list);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }

    @Override
    public List<SysUserRoleDO> selectByRoleId(Long roleId) {
        if (Objects.isNull(roleId)) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "角色ID不能为空");
        }
        return sysUserRoleMapper.selectByRoleId(roleId);
    }

    @Override
    public void deleteAuthUser(SysUserRoleVO sysUserRoleVO) {
        SysUserRoleDO sysUserRoleDO = TransformUtils.transform(sysUserRoleVO, SysUserRoleDO.class);

        int row;
        try {
            row = sysUserRoleMapper.deleteUserRoleInfo(sysUserRoleDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102, e);
        }

        if (row != 1) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102);
        }
    }
}
